{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T15:05:35.383812Z",
     "start_time": "2025-08-18T15:05:22.052648Z"
    }
   },
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "import os\n",
    "chatLLM = ChatOpenAI(\n",
    "    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "    api_key=os.getenv(\"DASH_SCOPE_API_KEY\"), # 如何获取API Key：https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key\n",
    "    base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    model=\"qwen-plus\",\n",
    "    temperature=0.8,\n",
    "    max_tokens=60,\n",
    ")\n",
    "\n",
    "# 原始字符串模板\n",
    "template = \"{flower}的花语是?\"\n",
    "# 创建LangChain模板\n",
    "prompt_temp = PromptTemplate.from_template(template) \n",
    "# 根据模板创建提示\n",
    "prompt = prompt_temp.format(flower='玫瑰')\n",
    "# 打印提示的内容\n",
    "print(prompt)\n",
    "\n",
    "output = chatLLM.invoke(prompt)\n",
    "\n",
    "print(output.content)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "玫瑰的花语是?\n",
      "玫瑰的花语因颜色和数量的不同而有所差异，以下是一些常见的玫瑰花语：\n",
      "\n",
      "### 按颜色分类：\n",
      "- **红玫瑰**：最经典的代表**爱情、热情、浪漫**，尤其是表达“我爱你”。\n",
      "- **粉玫瑰**：表示**温柔的爱、感激、欣赏**，也可以代表初恋或优雅。\n",
      "- **白玫瑰**：象征**纯洁、尊敬、真诚的爱**，也常用于婚礼和悼念场合。\n",
      "- **黄玫瑰**：代表**友谊、快乐、祝福**，是表达友情的好选择；但有时也象征嫉妒（旧时含义，现代多为正面）。\n",
      "- **橙色玫瑰（或珊瑚玫瑰）**：表达**热情、渴望、钦佩**。\n",
      "- **紫玫瑰（或蓝玫瑰）**：象征**神秘、高贵、梦想成真**。\n",
      "- **黑玫瑰**：多用于艺术表达，象征**告别、转变、神秘**，有时也代表坚贞不屈的爱。\n",
      "\n",
      "### 按数量分类（以红玫瑰为主）：\n",
      "- **1朵**：**“你是我的唯一”**\n",
      "- **3朵**：**“我爱上你”**\n",
      "- **7朵**：**“我偷偷地爱着你”**\n",
      "- **9朵**：**“长久”或“唯一只爱你”**\n",
      "- **11朵**：**“一心一意、一心只爱你”**\n",
      "- **99朵**：**“天长地久”**\n",
      "- **101朵**：**“至死不渝的爱”**\n",
      "- **999朵**：**“爱到天荒地老”**\n",
      "\n",
      "不同文化背景下，玫瑰的花语也可能略有不同。在中国，玫瑰多用于表达爱情；而在西方，玫瑰也被广泛用于纪念、致敬、祝福等场合。\n",
      "\n",
      "如果你有特定颜色或数量想了解，我可以进一步为你解释。🌹\n"
     ]
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
